//
//  MoveCarSettingViewController.m
//  carcareIOS
//
//  Created by 王然 on 16/9/18.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MoveCarSettingViewController.h"
#import "NotificationTableViewCell.h"

#define SECTION_HEIGHT (40)

@interface MoveCarSettingViewController ()

@property (nonatomic , strong) BaseViewModel <MoveCarSettingViewModelDelegate> * viewModel;

@end

@implementation MoveCarSettingViewController
- (instancetype)initWithViewModel:(BaseViewModel<MoveCarSettingViewModelDelegate> *)viewModel
{
    self = [super initWithTableRefreshType:TYPE_TABLE_REFRESH_NONE];
    if (self) {
        self.viewModel = viewModel;
        self.viewModel.viewController = self;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"通知设置";
    
    UIWindow *mainWindow = [UIApplication sharedApplication].delegate.window;
    self.timeV = [[[MoveCarSettingTimeV alloc]init] setupOnView:mainWindow];
    [self.timeV setHidden:YES];
    
    self.tableView.frame = CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64);
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.scrollEnabled = YES;
    self.tableView.separatorColor = [UIColor clearColor];
    
}
-(NSArray *)titleArray
{
    return @[@[@"勿扰模式"],@[@"时间段cell"],@[@"“勿扰模式”启用后,您将不会接到电话通知。"]];
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.titleArray[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (indexPath.section == 2 ? SECTION_HEIGHT  : (indexPath.section == 1 && [([MoveCarManager sharedInstance].setting).flag boolValue] == NO) ? 0.5 : CELL_HEIGHT) ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell;
    switch (indexPath.section) {
        case 0:
        {
            static NSString * cellID = @"cellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[NotificationTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            }
            [((NotificationTableViewCell *)cell) reloadBigCellHeight:CELL_HEIGHT];
            ((NotificationTableViewCell *)cell).titleStr = self.titleArray[indexPath.section][indexPath.row];
            ((NotificationTableViewCell *)cell).switchClick.onTintColor = [Theme mainColor];
            ((NotificationTableViewCell *)cell).switchClick.on = [([MoveCarManager sharedInstance].setting).flag boolValue];
            ((NotificationTableViewCell *)cell).detailStr = @"";
            
            [((NotificationTableViewCell *)cell).switchClick addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventTouchUpInside];
            
        }
            break;
        case 1:
        {
            NSString * cellID = @"timeCellID" ;
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[MoveCarTimeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                
            }
            ((MoveCarTimeCell *)cell).isHiddenTimeCell = ![([MoveCarManager sharedInstance].setting).flag boolValue];
            [((MoveCarTimeCell *)cell) reloadDataWithModel:[MoveCarManager sharedInstance].setting];
        }
            break;
            //文字cell
        case 2:
        {
            NSString * cellID = @"noChoiceCellID" ;
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                
                [[UILabel labelWithFrame:CGRectMake(cellLeft, 0, __MAIN_WIDTH - cellLeft * 2, SECTION_HEIGHT) font:FONT_DEFAULT_Light(13) text:self.titleArray[indexPath.section][indexPath.row] textColor:COLOR_HEXSTRING(@"b5b5b5")] setupOnView:cell];
            }
        }
            break;
        default:
            break;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        [self.viewModel clickOnTimeCell:(MoveCarTimeCell *)[tableView cellForRowAtIndexPath:indexPath]];
    }
}
- (void)switchClick:(UISwitch *)switchSender
{
    BOOL isSwitchOn = [switchSender isOn];
    
    [self.viewModel clickOnSwitch:isSwitchOn];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
